<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象存储</title>
    <!-- 引入Bootstrap CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container {
            max-width: 800px;
            margin: 50px auto;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }

        .pagination button {
            margin: 0 5px;
        }

        .table {
            margin-top: 20px;
        }

        /* Toast 样式 */
        .toast-container {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1050;
        }
    </style>
</head>
<body>
<div class="container">
    <h2 class="text-center">上传文件</h2>
    <form id="uploadForm" action="/add" method="post" enctype="multipart/form-data">
        <div class="mb-3">
            <label for="fileInput" class="form-label">选择文件:</label>
            <input type="file" class="form-control" id="fileInput" name="file" required>
        </div>
        <button type="submit" class="btn btn-primary">上传</button>
    </form>

    <h1 class="text-center">文件列表</h1>
    <button onclick="flushed()" class="btn btn-secondary mb-3">刷新</button>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>文件名</th>
            <th>展示URL</th>
            <th>下载文件</th>
            <th>删除URL</th>
            <th>Type</th>
        </tr>
        </thead>
        <tbody id="fileTable"></tbody>
    </table>
    <div class="pagination">
        <button onclick="prevPage()" class="btn btn-outline-primary">上一个</button>
        <span id="pageInfo" class="mx-3 align-self-center">Page 1 of 1</span>
        <button onclick="nextPage()" class="btn btn-outline-primary">下一个</button>
    </div>
</div>
<!-- Toast 提示 -->
<div class="toast-container">
    <div id="toast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
            <strong class="me-auto">提示</strong>
            <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        <div class="toast-body"></div>
    </div>
</div>
</body>


<!-- 引入Bootstrap JS -->
<script src="./js/bootstrap.bundle.min.js"></script>
<script>
    // 初始化 Toast
    const toastEl = document.getElementById('toast');
    const toast = new bootstrap.Toast(toastEl);

    // 显示 Toast 消息
    function showToast(message, type = 'info') {
        const toastBody = toastEl.querySelector('.toast-body');
        toastBody.textContent = message;
        toastEl.classList.remove('bg-info', 'bg-success', 'bg-danger');
        if (type === 'success') {
            toastEl.classList.add('bg-success', 'text-white');
        } else if (type === 'error') {
            toastEl.classList.add('bg-danger', 'text-white');
        } else {
            toastEl.classList.add('bg-info', 'text-white');
        }
        toast.delay = 500; // 0.5秒后消失
        toast.show();
    }


    // 当前页面
    let currentPage = 1;
    // 每页项目数
    const itemsPerPage = 5;
    // 总页数
    let totalPages = 0;
    //上传文件
    document.getElementById('uploadForm').onsubmit = async function (event) {
        event.preventDefault(); // 阻止默认表单提交
        const formData = new FormData(this);

        try {
            const response = await fetch('/add', {
                method: 'POST',
                body: formData
            });
            const data = await response.json();
            console.log(data)
            showToast('文件上传成功', 'success');
            flushed();
        } catch (error) {
            showToast('上传文件失败', 'error');
        }
    }

    //刷新页面
    function flushed() {
        loadPage(currentPage);
    }

    // 获取数据
    async function fetchData(Current) {
        const response = await fetch(`/getList?Current=${Current}&limit=${itemsPerPage}`);
        return await response.json();
    }

    // 删除文件
    async function deleteFile(url) {
        const response = await fetch(url);
        const data = await response.json();
        console.log(data.success)
        if (data.success) {
            showToast('删除成功', 'success');
            flushed();
        } else {
            showToast('删除失败', 'error');
        }
    }

    function renderTable(data) {
        const tableBody = document.querySelector('#fileTable');
        tableBody.innerHTML = ''; // Clear existing rows

        data.forEach(item => {
            const tr = document.createElement('tr');

            tr.innerHTML = `
            <td>${item.fileName}</td>
            <td><a href="${item.url}" target="_blank">点击查看</a></td>
            <td><a href="${item.downloadUrl}" target="_blank">点击下载</a></td>
            <td><button onclick="deleteFile('${item.deleteUrl}')">删除</button> </td>
            <td>${item.type}</td>
            `;
            tableBody.appendChild(tr);
        });
        document.getElementById('pageInfo').innerText = `Page ${currentPage} of ${totalPages}`;
    }

    async function loadPage(page) {
        const data = await fetchData(page);
        totalPages = data.data.total;
        renderTable(data.data.data);
    }

    function nextPage() {
        if (currentPage < totalPages) {
            currentPage++;
            loadPage(currentPage);
        }
    }

    function prevPage() {
        if (currentPage > 1) {
            currentPage--;
            loadPage(currentPage);
        }
    }

    // 初始加载
    loadPage(currentPage);
</script>

</html>
